package algorithm.recursionAndbacktrack.combination.p17;

import java.util.ArrayList;
import java.util.List;
/**
 * 17. 电话号码的字母组合
 */
class Solution_v2 {
    public String[] letters = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    public List<String> letterCombinations(String digits) {
        List<String> result = new ArrayList<>();
        if(digits == null || digits.length() == 0){
            return result;
        }
        StringBuilder sb = new StringBuilder();
        dfs(result, sb, digits, 0);
        return result;
    }

    public void dfs(List<String> result, StringBuilder sb, String digits, int index){
        if(sb.length() == digits.length()){
            result.add(sb.toString());
            return;
        }
        int num = digits.charAt(index) - '0';
        String letter = letters[num];
        for(int i = 0; i < letter.length(); i++){
            sb.append(letter.charAt(i));
            dfs(result, sb, digits, index + 1);
            sb.deleteCharAt(sb.length() - 1);
        }
    }
}